--
-- @Author:      name
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 消息的处理

local skynet = require "skynet"
local log = require "Log"
local config = require "configquery"
local NodeMessage = require "NodeMessage"
require "common"
local config = require "configquery"
local sharedata = require "sharedata"

local AssertEx = AssertEx
local Command = class("Command")
---------------------------------------------------------
-- Private
---------------------------------------------------------
function Command:ctor(message)
	self.msg = message	
	self.tbUser = {} --用户信息[playerid] = {}, 用户在那个gate进程
	self.nodeMessage = NodeMessage.new()
	self.dbmgr = nil
	self.roomConfig = {}
	self:register()
end

--注册本服务里的消息
function Command:register()
	self.msg:registerSelf('start',handler(self,self.start))
	self.msg:registerSelf('set_user_info',handler(self,self.setUserInfo))
	self.msg:registerSelf('get_user_info',handler(self,self.getUserInfo))
	self.msg:registerSelf('send_gate',handler(self,self.sendGate))
	self.msg:registerSelf('call_gate',handler(self,self.callGate))
	self.msg:registerSelf('send_user',handler(self,self.sendUser))

end


--加载游戏配置
function Command:loadConfig()
	local sqlStr = "select * from common_room"	
	local data = self.nodeMessage:callService("mysql_service",'execute',sqlStr)
	local configData = {}
	for k,v in pairs(data) do

		if g_gameType[v.gameid] and next(g_gameType[v.gameid]) then  
			local shardName = g_gameType[v.gameid].cfgRoom		
			configData[v.sub_id] = v
			if configData and next(configData) then 			
				if not self.roomConfig[shardName] then 					
			 		sharedata.new(shardName, configData)		 		
			 		self.roomConfig[shardName] = configData
			    else
			        sharedata.update(shardName, configData)
			        self.roomConfig[shardName] = configData
			    end		
			end	    
		end
		-- print("_________loadConfig___",shardName,self.roomConfig[shardName])
	end
	-- print("_____22____loadConfig__end___")
end

function Command:loop()
	while true do 
		skynet.sleep(600)
		self:loadConfig()
	end
end

---------------------------------------------------------
-- CMD
---------------------------------------------------------
function Command:start()
	--数据库
	local tables = {				
		-- { name="private_tables", pk='id',key='tableid' },
		-- { name="private_num", pk='id',key='server_gameid' },
	}
	local dbmgr = skynet.uniqueservice("dbmgr_service")
	skynet.call(dbmgr, "lua", "start", tables)
	self.dbmgr = dbmgr
	--mysql日记操作服务
	local logData = {host = skynet.getenv("mysql_host"),
				  port = skynet.getenv("mysql_port"),				  
				  user = skynet.getenv("mysql_user"),
				  password = skynet.getenv("mysql_pwd"),
				  database = "qicaipoker_log",
				  serviceName = "mysql_log",
				 }
	self.mysqlLog = skynet.newservice("mysql_service")
	skynet.call(self.mysqlLog, "lua", "start", logData)
	
	self:loadConfig()
	skynet.fork(function()
		self:loop()
	end)
	--后台服务
	skynet.uniqueservice("backstage_service")
	--所有桌子状态的管理
	skynet.uniqueservice("tablestate_service") 
	--广播服
	skynet.uniqueservice("bulletin_service")
	--机器人通知
	skynet.uniqueservice("robot_service")	
end


--设置用户信息
function Command:setUserInfo(playerid,data)
	if not self.tbUser[playerid] then 
		self.tbUser[playerid] = {}
	end
	if not data then 
		--释放用户数据
		self.tbUser[playerid] = nil
		return
	end
	for k,v in pairs(data) do
		self.tbUser[playerid][k] = v
	end
	--print("__________self.tbUser____",self.tbUser)
end

--取用户信息
function Command:getUserInfo(playerid)
	return self.tbUser[playerid]
end

--异步发送消息到用户所在gate进程
function Command:sendGate(playerid,...)
	local user = self.tbUser[playerid]
	-- print("______user___",user)
	if user and user.nodename then 
		self.nodeMessage:sendGate(user.nodename,playerid,...)
	else
		print("_____________用户没有登录_______________")

	end
end

--同步发送消息到用户所在gate进程
function Command:callGate(playerid,...)
	local user = self.tbUser[playerid]
	if user and user.nodename then 
		return self.nodeMessage:callGate(user.nodename,playerid,...)
	else
		print("_____________用户没有登录_______________")
		return false
	end
end




--给指定用户发消息
function Command:sendUser(playerid,...)
	local user = self.tbUser[playerid]
	if user and user.nodename then 
		self.nodeMessage:broadcastGate(user.nodename,{playerid},...)
	else
		print("_____________用户没有登录_______________")
	end
end


return Command